﻿using Book.IRepositories;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Book.Controllers
{
    [Route("api/achievements")]
    [ApiController]
    [EnableCors("any")]
    public class AchievementsController : ControllerBase
    {
        private readonly IAchievementsRepository _achievementsRepository;

        public AchievementsController(IAchievementsRepository achievementsRepository)
        {
            this._achievementsRepository = achievementsRepository;
        }
        [HttpPost("addachievements")]
        public IActionResult AddAchievements([FromBody] JObject parameter)
        {
            string abstracts = parameter.Value<string>("abstracts");
            string year = parameter.Value<string>("year");
            string link = parameter.Value<string>("link");
            string code = parameter.Value<string>("code");
            string title = parameter.Value<string>("title");
            string category = parameter.Value<string>("category");
            _achievementsRepository.AddAchievements(title, abstracts, year, link, code,category);
            return Ok();
        }
        [HttpGet("getachievements")]
        public IActionResult GetAchievements()
        {
            var rel = _achievementsRepository.GetAchievements();
            return Ok(rel);
        }
        [HttpPost("updateachievements")]
        public IActionResult UpdateAchievements([FromBody] JObject parameter)
        {
            string id= parameter.Value<string>("id");
            string abstracts = parameter.Value<string>("abstracts");
            string year = parameter.Value<string>("year");
            string link = parameter.Value<string>("link");
            string code = parameter.Value<string>("code");
            string title = parameter.Value<string>("title");
            string category = parameter.Value<string>("category");
            _achievementsRepository.UpdateAchievements(id, title, abstracts, year, link, code, category);
            return Ok();
        }
        [HttpPost("deleteachievements")]
        public IActionResult DeleteAchievements([FromBody] JObject parameter)
        {
            string id = parameter.Value<string>("id");
            _achievementsRepository.DeleteAchievements(id);
            return Ok();
        }
        [HttpPost("getachievementsbycategory")]
        public IActionResult GetAchievementsByCategory([FromBody] JObject parameter)
        {
            string category = parameter.Value<string>("category");
            var rel = _achievementsRepository.GetAchievementsByCategory(category);
            return Ok(rel);
        }
        [HttpPost("getyearbycategory")]
        public IActionResult GetYearByCategory([FromBody] JObject parameter)
        {
            string category = parameter.Value<string>("category");
            var rel = _achievementsRepository.GetYearByCategory(category);
            return Ok(rel);
        }
        [HttpPost("getachievementsByyear")]
        public IActionResult GetAchievementsByYear([FromBody] string year)
        {
           // string category = parameter.Value();
            var rel = _achievementsRepository.GetAchievementsByYear(year);
            return Ok(rel);
        }

    }
}
